package com.copa.gameStore.service.impl;

import com.copa.gameStore.domain.GameNews;
import com.copa.gameStore.domain.GameNewsDetail;
import com.copa.gameStore.mapper.GameNewsDetailMapper;
import com.copa.gameStore.service.GameNewsDetailService;
import com.copa.gameStore.service.GameNewsService;
import com.copa.gameStore.utils.GameTimeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author copa
 * @createDate 2022-09-25 21:35
 * @function
 */
@Service
public class GameNewsDetailServiceImpl implements GameNewsDetailService {

    @Resource
    private GameNewsDetailMapper gameNewsDetailMapper;

    @Resource
    private GameNewsService gameNewsService;

    @Resource
    private GameTimeUtils gameTimeUtils;

    private final static Integer YEAR = 3;

    @Override
    public void add() {
        // 假设就搜索这三年的，今年为第二年
        int year = LocalDate.now().getYear() - 1;
        for (int i = 0; i < YEAR; i++) {
            List<GameNews> gameList = gameNewsService.findBySaleTimeYear(year);
            if (Objects.isNull(gameList) || gameList.isEmpty()) {
                continue;
            }
            gameList.forEach(gameNews ->  {
                GameNewsDetail gameDetail = gameTimeUtils.getGamesInfoDetail(gameNews.getDetailUrl());
                gameDetail.setGameNewsId(gameNews.getId());
                gameNewsDetailMapper.insert(gameDetail);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            year++;
        }
    }
}
